using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
using System.Linq;


public class AddressableClass 
{

    string[] baseDirs = new string[] { "Local", "Remoted" };

    public StringBuilder builder = new StringBuilder();
    public List<AddressableCustomGroup> addressableCustoms = new List<AddressableCustomGroup>();

    List<string> filter = new List<string>()
    {
      "Log"
    };

    public bool Intersect(string name)
    {
        foreach (var item in filter)
        {
            if (name.Contains(item))
                return true;
        }
        return false;
    }

    public AddressableClass(string folder, int order)
    {

        if (!Directory.Exists(Application.dataPath + "/AddressableAssets"))
        {
            Debug.LogError($"�������ļ��� AddressableAssets");
            return;
        }

        DirectoryInfo directory = new DirectoryInfo(folder);

        if (directory == null)
        {
            Debug.LogError($"�������ļ���{folder}");
            return;
        }


        foreach (var item in baseDirs)
        {
            DirectoryInfo itemDir = new DirectoryInfo($"{directory.FullName}/{item}");

            if (itemDir == null)
            {
                Debug.LogError($"�������ļ���{folder}");
                continue;
            }

            var groupDirs = itemDir.GetDirectories();
            foreach (var dir in groupDirs)
            {
                string repl = (AddressableBuildTools.AppDataPath + "\\").Replace('\\', '/');
                string name = $"{dir.FullName}/CustomGroup.asset".Replace('\\', '/').Replace(repl,"");
                var custom = AssetDatabase.LoadAssetAtPath<AddressableCustomGroup>(name);
                addressableCustoms.Add(custom);
            }
        }

        foreach (var item in AddressableBuildTools.assetSettings.groups)
        {
           foreach (var entry in item.entries)
           {
                Debug.Log(entry);
            }
        }



        var className  = directory.Name == CreatUtil.Addressable_FD ? "AddressFolder" : CreatUtil.GetClassString(directory.Name);

        builder.Clear();
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}//�Զ������࣬�����޸�");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}public class {className}");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.LeftSymbol}");

       var  classOrder = order+ 5;

        foreach (var item in addressableCustoms)
        {
 
            var type = item.addressType == AddressType.Remoted ? "r" : "l";
            var classGroup = FormatFiled(item.groupName.Substring(0, 1).ToUpper() + item.groupName.Substring(1));
            builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}//�Զ������࣬�����޸�");
            builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}public class {type}{classGroup}");
            builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}{CreatUtil.LeftSymbol}");

            var group = $"{item.addressType.ToString()}_{item.groupName}";
            var groupAddress = AddressableBuildTools.assetSettings.groups.Where(_ => _.name.Contains(group)).First();
            var paths = groupAddress.entries.Select(_ => _.AssetPath.Replace('\\', '/')).ToList();

            EditorUtility.SetDirty(item);

            foreach (var label in item.labels)
            {
                string labelType = string.Empty;
                if (label.labelName != "default") labelType = "_" + label.labelName;

                foreach (var entry in label.entryInfos)
                {
                    if (paths.Contains(entry.addressPath))
                    {
                        paths.Remove(entry.addressPath);
                    }

                    string filed = label.isSampleName ? entry.address : Path.GetFileName(entry.address);
                    filed = FormatFiled(filed  + labelType);

                    builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}{CreatUtil.SetStringField(filed, entry.address)}");
                }
            }

            var finds = groupAddress.entries.Where(_ => paths.Contains(_.AssetPath.Replace('\\', '/'))).ToList();
            foreach (var find in finds)
            {
                string labelType = string.Empty;
                foreach (var label in find.labels)
                {
                    labelType += "_" + label;
                }

                string filed = Path.GetFileName(find.address);
                filed = FormatFiled(filed + labelType);

                builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}{CreatUtil.SetStringField(filed, find.address)}");
            }


            builder.AppendLine($"  {CreatUtil.GetFormat(classOrder)}{CreatUtil.RightSymbol}");
        }
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.RightSymbol}");
    }


    string FormatFiled(string filed)
    {
      return   filed
            .Replace('-', '_')
            .Replace('.', 'ؼ')
            .Replace("(", "")
            .Replace(")","");
    }
}

